package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.IntentHelper;
import com.yxq.mdt.app.constant.RouteTable;
import com.yxq.mdt.app.modules.shoppingcart.bean.CartBean;
import com.yxq.mdt.app.modules.shoppingcart.ui.CartListAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 确认订单-商品清单
 */
public class OrderConfirmGoodsListBottomSheetDialogFragment extends BottomSheetDialogFragment {

    public OrderConfirmGoodsListBottomSheetDialogFragment(List<CartBean.ListBean> listBeanList) {
        this.listBeanList = listBeanList;
    }

    Unbinder unbinder;
    @BindView(R.id.order_confirm_tv_goods_count)
    TextView orderConfirmTvGoodsCount;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    List<CartBean.ListBean> listBeanList;
    CartListAdapter cartListAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_order_confirm_goods_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        view.findViewById(R.id.goods_attr_iv_close).setOnClickListener(view1 -> dismiss());

    }

    public void setData() {
        if (listBeanList == null) {
            return;
        }
        int num = 0;
        for (CartBean.ListBean bean : listBeanList) {
            num += bean.getNum();
        }
        orderConfirmTvGoodsCount.setText(String.format("共%d件", num));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        cartListAdapter = new CartListAdapter(R.layout.layout_order_confirm_goods_item_layout, listBeanList);
        cartListAdapter.setShowCheckBox(false);
        recyclerView.setAdapter(cartListAdapter);
        cartListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putString("id", cartListAdapter.getItem(position).getGoods_id() + "");
            IntentHelper.arouterUri(RouteTable.ROUTER_ACTIVITY_GOODS_DETAIL, bundle);
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        setData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
